#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QString>
#include<QDebug>
#include<QPaintEvent>
#include<QSound>
#include<smoke.h>
#include<QCoreApplication>


int MainWindow::random(int a,int b)
{

    qint16 rand= QRandomGenerator::global()->bounded(a,b);//获取a-b的随机数
    return rand;
}


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    paintEvent(this->e);
    in();//初始化
    game();//初始化游戏
    Time.setInterval(50);
    Time.start();
}


MainWindow::~MainWindow()
{
    delete ui;
}

//void MainWindow::moveImage()
//{
//    QPixmap pixmap("path/to/image.png");

//           // 创建一个QPainter对象并设置画笔
//           QPainter painter(&pixmap);
//    int dx = (rand() % 10) - 5; // 每次移动5个像素
//            int dy = (rand() % 10) - 5; // 每次移动5个像素
//            QPoint newPos(width() / 2 + dx, height() / 2 + dy);
//            pixmap.move(newPos);

//            // 如果已经到达窗口的边缘，则将图片移回中心位置
//            if (newPos.x() < 0 || newPos.x() >= width())
//            {
//                newPos.setX(width() / 2);
//            }
//            else if (newPos.y() < 0 || newPos.y() >= height())
//            {
//                newPos.setY(height() / 2);
//            }

//            // 重新绘制QPixmap对象以更新显示内容
//            painter.begin(&pixmap);
//            painter.drawPixmap(newPos, pixmap);
//            painter.end();
//}

void MainWindow::in()
{
    //QSound::play();//播放音乐
    //    this->move(400,300);
    this->setFixedSize(1500,900);
    qDebug()<<"初始化";
    QPainter* painter=new QPainter(this);

    QPixmap ground(":/Saved Pictures/litanggrass.png");
    ground=ground.scaled(1500,900);//缩放
    QPixmap back("://Saved Pictures/furongdao.png");
    back=back.scaled(1500,120);//缩放
    painter->drawPixmap(0,0,ground);


    Smoke s;
    timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));
//    connect(&Time, &QTimer::timeout(), this, moveImage);
    Smoke *S=new Smoke();
    S->insmoke();
    painter->end();
    delete S;
    delete painter;
}


void MainWindow::smokedraw()
{
    QPainter* painter=new QPainter(this);
//    int seed_x=static_cast<int>(clock());
//  furong=QPixmap(QApplication::applicationDirPath().append(""));
    int _2X=224;//第二个香烟x坐标
//    int _3X=0;//第三个香烟x坐标


//    for(int a=0;a<l;a++)
//    {
        Smoke* SM=new Smoke();
//        if(a==0)
//        {
//            //_3X=(*sm)[1]->getx();
//            _2X=(*sm)[1]->getx();
//        }
//        else
//            {
//                //_3X=(*sm)[0]->getx();
//                _2X=(*sm)[0]->getx();
//            }
//            else
//            {
//                _3X=(*sm)[2]->getx();
//                _2X=(*sm)[2]->getx();
//            }
        int h=random(10,700);
        static int x1=900;
        if(x1>0)
        {
            x1-=5;
            SM->setx(x1);
            qDebug()<<x1;
            SM->draw(*painter);
//            update();
        }
        else
        {
            SM->seth1(h);//设置高度
            x1=1200;//设置位置
            qDebug()<<h;
            SM->insmoke();//初始化
//            update();
        }
//          SM->setx(x);
//    }
//    Smoke* sm;
//    sm->setx(_1X);
//    qDebug()<<_1X;
//    sm->draw(*painter);
//    _1X-=80;
        painter->end();
        delete painter;
        delete SM;

}


void MainWindow::speed()
{
    downSpeed=1.0;
    upSpeed=20.0;
}

void MainWindow::game()
{
    in();
    speed();
    dzStatus=DZ::down;
    timer=new QTimer(this);
    timer->start(16);

}

void MainWindow::endgame()
{
    timer->stop();
    gameStatus=GAME::STOP;
    downSpeed=0;
    upSpeed=0;

}

void MainWindow::paintEvent(QPaintEvent *ev)
{

    QPainter* painter=new QPainter(this);
    in();
    qDebug()<<"绘图";
    backdraw();
    smokedraw();
    dzdraw();//绘制背景图，芙蓉王障碍，丁真角色
    update();
    delete painter;

}

void MainWindow::backdraw()//绘制背景
{
    QPainter* painter=new QPainter(this);
    QImage back("://Saved Pictures/furongdao.png");
    back=back.scaled(1500,120);//缩放
    if(i<-1200)
    {
        i=0;
        painter->drawImage(i,780,back);
        i-=10;
        painter->drawImage(1200+i,780,back);
    }
    else
    {
        painter->drawImage(i,780,back);
        i-=10;
        painter->drawImage(1200+i,780,back);
    }
    painter->end();
    delete painter;
}

void MainWindow::dzdraw()//绘制角色
{


    dz1=new QPixmap("://Saved Pictures/dingzhen.png");
    dz3=new QPixmap("://Saved Pictures/dingzhencry.png");


    QPainter* painter=new QPainter(this);

    QPixmap dz("://Saved Pictures/dingzhen.png");//绘制丁真
    dz=dz.scaled(40,55);//缩放

    if(dzStatus==DZ::down)
    {
        dzy+=downSpeed;//实现下降绘制
        downSpeed+=0.2;//下降加速度

    }
    if(dzStatus==DZ::up)
    {
        dzy=dzy-upSpeed;
        upSpeed-=2.0;

        if(upSpeed<=0.0)
        {
            dzStatus=DZ::down;
            speed();
        }
    }

    dzx=500;
    painter->drawPixmap(dzx,dzy,dz);
    if(dzy>=780||dzy<=0)
    {
        dz=*dz3;
        painter->drawPixmap(100,100,dz);
        endgame();
    }

    painter->end();
    delete painter;
}

void MainWindow::mousePressEvent(QMouseEvent *)
{
    qDebug()<<"点击";
    //QSound::play(WINGPATH);//添加声音，每次点击
    dzStatus=DZ::up;
    in();
    speed();
}
void MainWindow::cirPaint()
{
    update();
}

void MainWindow::cirgame()
{
    game();
}
